//
// entry.cpp
// libprotea
// Tools Library for the Protea Project / Functionality for entry points of libraries/executables/processes/threads
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include "defines.h"
#include "entry.h"
#include "errorhandler.h"
#include "Appdic/appdic.h"
#include "AppHandler/security.h"
#include "AppHandler/printoutput.h"
#include "OSDep/registry.h"
#include "OSDep/files.h"

#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/Include/PlatformDefinitions.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>
XALAN_USING_XALAN(XalanTransformer)

ENTRY_CRYPT_PROVIDER;

#include "version.h"

// Embed the Protea Project version
const char Protea_VersionId[] = VERSION_TAG;

extern "C" {
__EXPORT const char* ProteaVersion() {
	return Protea_VersionId;
};
};

/** \mainpage The Protea Project SDK Documentation

	\section intro Introduction

	This documentation details the Protea Project SDK.
*/


XCBase::~XCBase() {
	// DO NOTHING
};

ENTRY_MAIN;

//
// Initialization functions
//
bool xerces_inited = false;
bool xalan_inited = false;

void Protea::Initialize( bool init_xerces, bool init_xalan ) {
	if (init_xerces) {
		XERCES_CPP_NAMESPACE::XMLPlatformUtils::Initialize();
		xerces_inited = true;
	};

	if (init_xalan) {
		XalanTransformer::initialize();
		xalan_inited = true;
	};

	ENTRY_INIT;
};

void Protea::Uninitialize() {
	if (xalan_inited) {
		XalanTransformer::terminate();
		xalan_inited = false;
	};

	if (xerces_inited) {
		XERCES_CPP_NAMESPACE::XMLPlatformUtils::Terminate();
		xerces_inited = false;
	};

	ENTRY_UNINIT;

	// Clear out any functions on the stack
	XCClearCallStack();
};

void Protea::ThreadInitialize() {
	ENTRY_THREAD_INIT;
};

void Protea::ThreadUninitialize() {
	ENTRY_THREAD_UNINIT;

	// Clear out any functions on the stack
	XCClearCallStack();
};


void Protea::DebugOutput( const char* debug_str ) {
	DEBUGOUT( debug_str );
};

// Command Line intro
void Protea::ShowIntro( const char* filename )
{
	const char* versn = VERSION_NUMBER;
	printf( "%s, The Protea Project Version %s,\n", filename, versn );
	printf( "Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.\n" );
	printf( "Copyright (C) 2008 Bruce A. James\n\n");
#ifndef CLOSED_SOURCE
	printf( "The Protea Project comes with ABSOLUTELY NO WARRANTY;\n" );
	printf( "This is free software, and you are welcome to \n" );
	printf( "redistribute it under certain conditions;\n" );
#endif
	printf( "Type '%s -L' for license details.\n\n", filename );
};

// Display License
void Protea::ShowLicense()
{
	const char* LICENSE_FILE = "COPYING";
	char file[ MAX_PATH ];
	XCRegistry registry;

	char* xsiroot = registry.GetStringValue( "XSIROOT" );
	sprintf( file, "%s%s%s", xsiroot, OSFILEDELIM, LICENSE_FILE );
	free( xsiroot );

	if (XCFileExists( file )) 
	{
		XCFileTransport infile( NULL );
		infile.Open( file );
		long length = infile.GetSize();
		char buffer;
		for ( long ctr = 0; ctr < length; ctr++ )
		{
			infile.Recv( &buffer, 1 );
			printf( "%c", buffer );
		}
		infile.Close();
	} 
	else 
	{
		// File not found
		printf( "ERROR: %s file missing.\n", LICENSE_FILE );
	};
};
